/*
Copyright (c)2006-2007 - Brett Lajzer

See LICENSE for license information.
*/


#include <esdl/SDL.h>
#include <lua.h>
#include <lauxlib.h>
#include "util.h"
#include "funcs.h"


//prints usage info
void print_usage(const char* progname){
    char info[255];
	sprintf( info,"USAGE: %s [dir] | [-h|--help]\n",progname);
	printf(info);
	printf("    dir - directory where game data is\n");
    printf("    -h --help - displays this usage info\n\n");
}

//configuration loader
//loads config into lua, retrieves values and leaves them there
SDL_Surface *load_config(lua_State *L, const char* pname){
	SDL_Surface *surface;
	int i;
	int fullscreen,w,h,screenbpp;
	char configscript[1024];
	sprintf(configscript,"%s/%s%s",appPath,gamename,"/scripts/config.lua");

	if(luaL_loadfile(L, configscript) || lua_pcall(L,0,0,0)){
		UTIL_Error( "Cannot load configuration script!\nError: %s", lua_tostring(L, -1));
      print_usage(pname);
      return 0;
    }

    //put the config values on the stack
    lua_getglobal(L, "s_fullscreen");
    lua_getglobal(L, "s_width");
    lua_getglobal(L, "s_height");
    lua_getglobal(L, "s_depth");
    lua_getglobal(L, "s_gamename");
    
    //check values for proper type
    if(!lua_isnumber(L,-5)){
    UTIL_Error("Config Error: 's_fullscreen' should be a number\n");
      return 0;
    }
    if(!lua_isnumber(L,-4)){
    UTIL_Error( "Config Error: 's_width' should be a number\n");
      return 0;
    }
    if(!lua_isnumber(L,-3)){
    UTIL_Error( "Config Error: 's_height' should be a number\n");
      return 0;
    }
    if(!lua_isnumber(L,-2)){
    UTIL_Error( "Config Error: 's_depth' should be a number\n");
      return 0;
    }
    if(!lua_isstring(L,-1)){
    UTIL_Error( "Config Error: 's_gamename' should be a string\n");
      return 0;
    }
    w = lua_tointeger(L, -4);
    h = lua_tointeger(L, -3);
    
    clientRect.w = w;
    clientRect.h = h;
    
    screenbpp = lua_tointeger(L, -2);
    fullscreen = lua_tointeger(L, -5);
    
    //set caption and return the surface
    SDL_WM_SetCaption(lua_tostring(L,-1), lua_tostring(L, -1));
    
    //SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    surface = SDL_SetVideoMode(w,h, 0, SDL_SWSURFACE|fullscreen? SDL_FULLSCREEN : 0);
    g_surface = SDL_CreateRGBSurface(surface->flags & ~SDL_HWSURFACE, w, h, 32,
    		surface->format->Rmask, surface->format->Gmask,
    		surface->format->Bmask, surface->format->Amask);
    
    

    //surface = SDL_SetVideoMode(lua_tointeger(L, -4), lua_tointeger(L, -3), lua_tointeger(L, -2), SDL_OPENGL | (lua_tointeger(L,-5)? SDL_FULLSCREEN : 0));
    
    //initialize OpenGL
    	
	//cleanup
	for(i=0; i < 5; i++){lua_pop(L,-1);}
	
	return surface;
}
void free_image_value(void* pv)
{
    free(pv);
}
//function for clearing out the image store
void clear_images(){

hmap_destroy(image_store, free_image_value);
   // map<string, SDL_Surface *>::iterator im = image_store.begin();

//    for(; im != image_store.end(); im++){
//        glDeleteTextures(1, &(im->second->texture));
//        delete im->second;
//    }

    //image_store.clear();
}
